////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                           BailterRay                                       //
//                    Pip Robbins & Zhao Zhang                                //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
class BailterRaySinkingPlatform extends KActor
	placeable;

var vector PlatformLocation;
var rotator PlatformRotation;
var ParticleSystem DirtEffect;
var ParticleSystem FallingDirtEffect;
var ParticleSystemComponent Dirt;
var ParticleSystemComponent FallingDirt;

var BailterRayPawn GP;

var bool bIsKActor;
var bool btouched;


var float FallingTime;
var () int FallingScale;

simulated event PostBeginPlay()
{
    if(!bWakeOnLevelStart)
    {
        ClearTimer('TurnToKActor');
        ClearTimer('TurnToActor');
    	SetHidden(FALSE);
    	StaticMeshComponent.SetHidden(false);
        SetPhysics(PHYS_None);
        SetCollision(true, false);
        PlatformLocation = Location;
        PlatformRotation = Rotation;
    }
    super.PostBeginPlay();
}

simulated function ResetPlatform()
{
    SetHidden(false);
    StaticMeshComponent.SetHidden(false);
    SetPhysics(PHYS_None);
    SetCollision(true, false);
    StaticMeshComponent.SetRBPosition(PlatformLocation);
    StaticMeshComponent.setRBRotation(PlatformRotation);
    StaticMeshComponent.PutRigidBodyToSleep();
    setLocation(PlatformLocation);
    setRotation(PlatformRotation);
    bWakeOnLevelStart = false;

}

simulated function TurnToActor()
{
    `log("SinkingPlatform: TurnToActor()");
    SetHidden(false);
    StaticMeshComponent.SetHidden(false);
    SetPhysics(PHYS_None);
    StaticMeshComponent.SetRBPosition(Location);
    StaticMeshComponent.setRBRotation(Rotation);
    StaticMeshComponent.PutRigidBodyToSleep();
    setLocation(Location);
    setRotation(Rotation);
    bIsKActor = false;
    bWakeOnLevelStart = false;
}

event Detach( Actor Other )
{
	local Pawn P;
	P = Pawn(Other);

    if(!P.bIsCrouched)
        btouched = false;
}
event Attach( Actor Other )
{

    btouched = true;
    super.Attach(Other);
}
event Touch( Actor Other, PrimitiveComponent OtherComp, vector HitLocation, vector HitNormal )
{
    local vector DirtLocation;
    local rotator DirtRotation;

    GP = BailterRayPawn(Other);

    if(!bIsKActor && self.Location == PlatformLocation && self.Rotation == PlatformRotation)
    {
        if( GP != None && ValidTouch(GP))
        {
            if(StaticMeshComponent!=none )
            {
                btouched = true;
                `log("SinkingPlatform: Touched");
                SetCollision(true, true);
                if(GP.Mesh.GetSocketWorldLocationAndRotation(GP.BootSockets[1], DirtLocation, DirtRotation))
                {
                     DirtLocation.Y -=60;
                     DirtRotation.Pitch = Rand(18364);
                     DirtRotation.Yaw = Rand(18364);
                     DirtRotation.Roll = Rand(18364);
                     Dirt = WorldInfo.MyEmitterPool.SpawnEmitter(DirtEffect, DirtLocation, DirtRotation);
                     Dirt.SetScale(0.06);
                }
                SetTimer(1, false, 'DestroyDirt');
                FallingTime = 0.13 * FallingScale;
                setTimer(0.08,false,'TurnToKActor');

            }
        }
        super.Touch(Other, OtherComp, HitLocation, HitNormal);
    }
}


simulated function DestroyDirt()
{
     Dirt.SetActive(false);
}

simulated exec function TurnToKActor()
{
    local vector momentum;
    `log("SinkingPLatform: TurnToKActor");
    SetPhysics(PHYS_RigidBody);
    StaticMeshComponent.WakeRigidBody();
    if(GP!=None)
    {
        momentum = GP.Velocity;
        StaticMeshComponent.AddImpulse(momentum , Location);
    }
    PlatformLocation = Location;
    PlatformRotation = Rotation;
    bIsKActor=true;
    setTimer(FallingTime,false,'TurnToActor');
}

function bool ValidTouch( Pawn Other )
{
    if(Other.IsHumanControlled())
        return true;
    return false;
}


function Tick(float DeltaTime)
{
    local BailterRayPlayerController PC;
    local BailterRayPawn BP;


    foreach LocalPlayerControllers(class'BailterRayPlayerController',PC)
    {
         if(PC.IsPlayerOwned())
             break;
    }
    BP=BailterRayPawn(PC.ViewTarget);

    if(BP != none)
    {
        if(!btouched)
        {
            ReadyToBeLanded();
        }

        Respawn(BP);
    }
}

function Respawn(Pawn P)
{
    if(P != none)
    {
        if(P.Health <= 0)
        {
            if(bIsKActor)
            {
                ResetPlatform();
                bIsKActor=false;
            }
        }
    }
}

simulated function ReadyToBeLanded()
{
     SetHidden(false);
     StaticMeshComponent.SetHidden(false);
     SetPhysics(PHYS_None);
     SetCollision(true, false);
     StaticMeshComponent.SetRBPosition(PlatformLocation);
     StaticMeshComponent.setRBRotation(PlatformRotation);
     setLocation(PlatformLocation);
     setRotation(PlatformRotation);
     bIsKActor = false;
     bWakeOnLevelStart = false;
}
defaultproperties
{
    DirtEffect=ParticleSystem'Envy_Effects.Dirt.Effects.P_EFX_Dirt_Rockbridge_Collapse'
    btouched=false
    FallingScale = 1;
    bIsKActor=false
    Begin Object name=StaticMeshComponent0
      StaticMesh=StaticMesh'BailterRayAssetPackage.Meshes.VolcanicPlatformMesh02'
    End Object
    StaticMeshComponent=StaticMeshComponent0
    Components(0)=MyLightEnvironment
    Components(1)=StaticMeshComponent0
    CollisionComponent=StaticMeshComponent0
    CollisionType=COLLIDE_BlockAll
}